﻿package mortal.game.view.friendBless.view
{
    import Message.Game.*;
    import baseEngine.modifiers.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.display.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.QuestionCopy.data.*;
    import mortal.game.view.friendBless.view.util.*;
    import mortal.mvc.core.*;

    public class FriendBlessBall extends Window
    {
        private var _ball:GBitmap;
        private var _blessNum:GTextFiled;
        private var _clickSpr:GSprite;
        private var _bmp:GBitmap;
        private var _swfPlayer:SWFPlayer;
        private var _effectPlayer:EffectPlayer;
        private var _rect3d:Rect3DObject;
        private var _bg2d:Img2D;
        private var _tmpHidePoint:Point;
        private static var _instance:FriendBlessBall;

        public function FriendBlessBall()
        {
            this.layer = LayerManager.entityTalkLayerParent;
            mouseEnabled = true;
            isForbidenDrag = true;
            return;
        }// end function

        public function hideForDrama(param1:Boolean = true) : void
        {
            if (param1)
            {
                if (this._tmpHidePoint == null)
                {
                    this._tmpHidePoint = new Point();
                }
                this._tmpHidePoint.x = this.x;
                this._tmpHidePoint.y = this.y;
                this.x = 4000;
            }
            else if (this._tmpHidePoint != null)
            {
                this.x = this._tmpHidePoint.x;
                this.y = this._tmpHidePoint.y;
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            _isAddClolseButton = false;
            super.createDisposedChildrenImpl();
            this._ball = UIFactory.gBitmap(ImagesConst.FriendBlessBall_Lv, 0, 0);
            this._clickSpr = UICompomentPool.getUICompoment(GSprite);
            this._clickSpr.mouseEnabled = true;
            this.contentTopOf3DSprite.addChild(this._clickSpr);
            this._bmp = UIFactory.gBitmap("", 30, 10, this._clickSpr);
            this._bmp.bitmapData = GlobalClass.getBitmapData(BitmapDataConst.AlphaBMD);
            this._bmp.width = 100 - 20;
            this._bmp.height = 114 - 10 - 30;
            this._blessNum = UIFactory.gTextField("", 16, 90 + 8, 110, 25, this._clickSpr, GlobalStyle.textFormatBai.center().setSize(14));
            this._clickSpr.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._clickSpr.configEventListener(MouseEvent.MOUSE_OVER, this.onOverHandler);
            this._clickSpr.configEventListener(MouseEvent.MOUSE_OUT, this.onOutHandler);
            this._swfPlayer = UIFactory.getUICompoment(SWFPlayer);
            this.addAroundEffect();
            this.updateBallVol();
            this.initRect3d();
            this.updateEffectPlayer();
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.FriendBlessCongratulateInfo, this.onCongratulateInfoUpdate);
            Dispatcher.addEventListener(EventName.StageResize, this.onStageResize);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.FriendBlessCongratulateInfo, this.onCongratulateInfoUpdate);
            Dispatcher.removeEventListener(EventName.StageResize, this.onStageResize);
            return;
        }// end function

        private function initRect3d() : void
        {
            this._rect3d = Rect3DManager.instance.registerWindow(false, new Rectangle(0, 0, 10, 10), this);
            Rect3DManager.instance.windowShowHander(null, this);
            if (this._bg2d)
            {
                this._bg2d.dispose(false);
            }
            var _loc_1:* = GlobalClass.getBitmapData(BitmapDataConst.AlphaBMD);
            this._bg2d = new Img2D(null, this._ball.bitmapData, new Rectangle(0, 0, this._ball.bitmapData.width, this._ball.bitmapData.height), true);
            this._bg2d.autoDispose = false;
            this._rect3d.addImg(this._bg2d);
            return;
        }// end function

        private function updateEffectPlayer() : void
        {
            var _loc_1:int = 0;
            var _loc_2:String = null;
            if (this._rect3d)
            {
                this.removeEffect();
                _loc_1 = Cache.instance.role.entityInfo.career;
                _loc_2 = "UI_test02";
                this._effectPlayer = EffectPlayerPool.instance.getEffectPlayer(_loc_2, null, this._rect3d.renderList, false);
                this._effectPlayer.play(PlayMode.ANIMATION_LOOP_MODE);
                this._rect3d.addObject3d(this._effectPlayer, 72 - 5 + 3, 48 + 2);
            }
            return;
        }// end function

        private function removeEffect() : void
        {
            if (this._effectPlayer)
            {
                this._rect3d.disposeObj3d(this._effectPlayer);
                this._effectPlayer = null;
            }
            return;
        }// end function

        private function onCongratulateInfoUpdate(param1:Object) : void
        {
            if (isDisposed)
            {
                return;
            }
            this.updateBallVol();
            return;
        }// end function

        private function updateBallVol() : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:Object = null;
            var _loc_5:int = 0;
            var _loc_6:Object = null;
            var _loc_7:int = 0;
            var _loc_1:* = Cache.instance.friendBless.sCongratulateInfo;
            if (_loc_1)
            {
                _loc_2 = QuestionCopyConfig.instance().getRewardInfoByType(5);
                for each (_loc_6 in _loc_2)
                {
                    
                    if (!_loc_6.scoreMin)
                    {
                        _loc_6.scoreMin = 0;
                    }
                    if (_loc_1.congratulateValue >= _loc_6.scoreMin && _loc_1.congratulateValue <= _loc_6.scoreMax)
                    {
                        _loc_3 = _loc_6.scoreMax as int;
                        _loc_4 = _loc_6;
                    }
                    _loc_5 = _loc_6.scoreMax > _loc_5 ? (_loc_6.scoreMax) : (_loc_5);
                }
                if (_loc_3 == 0)
                {
                    _loc_3 = _loc_5;
                }
                this._blessNum.text = _loc_1.congratulateValue + "/" + _loc_5;
                if (_loc_4)
                {
                    switch(_loc_4.scoreMax)
                    {
                        case 10:
                        {
                            this._ball.bitmapData = GlobalClass.getBitmapData(ImagesConst.FriendBlessBall_Lv);
                            break;
                        }
                        case 50:
                        {
                            this._ball.bitmapData = GlobalClass.getBitmapData(ImagesConst.FriendBlessBall_Lan);
                            break;
                        }
                        case 100:
                        {
                            this._ball.bitmapData = GlobalClass.getBitmapData(ImagesConst.FriendBlessBall_Zi);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                    _loc_7 = this.getTakeMaxValue((_loc_1.hadGain + 1));
                    if (_loc_4.scoreMax >= _loc_7)
                    {
                        this.isTakeAvailable = true;
                    }
                    else
                    {
                        this.isTakeAvailable = false;
                    }
                }
                else
                {
                    this._ball.bitmapData = GlobalClass.getBitmapData(ImagesConst.FriendBlessBall_Zi);
                }
                this.removeImg2d();
                this.addImg2d();
            }
            return;
        }// end function

        private function addImg2d() : void
        {
            if (this._rect3d)
            {
                this._bg2d = new Img2D(null, this._ball.bitmapData, new Rectangle(0, 0, this._ball.bitmapData.width, this._ball.bitmapData.height), true);
                this._bg2d.autoDispose = false;
                this._rect3d.addImg(this._bg2d);
            }
            return;
        }// end function

        private function removeImg2d() : void
        {
            if (this._rect3d)
            {
                this._rect3d.removeImg(this._bg2d);
                this._bg2d = null;
            }
            return;
        }// end function

        private function getTakeMaxValue(param1:int) : int
        {
            var _loc_2:int = 0;
            switch(param1)
            {
                case FriendBlessConst.HasTakenReward_None:
                {
                    _loc_2 = 10;
                    break;
                }
                case FriendBlessConst.HasTakenReward_Lv:
                {
                    _loc_2 = 50;
                    break;
                }
                case FriendBlessConst.HasTakenReward_Lan:
                {
                    _loc_2 = 100;
                    break;
                }
                case FriendBlessConst.HasTakenReward_Zi:
                {
                    break;
                }
                default:
                {
                    break;
                }
            }
            return _loc_2;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            if (FriendBlessBallWin.instance.isHide)
            {
                FriendBlessBallWin.instance.show();
            }
            return;
        }// end function

        private function onOverHandler(event:MouseEvent) : void
        {
            CursorManager.showCursor(CursorManager.BUTTON);
            return;
        }// end function

        private function onOutHandler(event:MouseEvent) : void
        {
            CursorManager.hideFromButton();
            return;
        }// end function

        private function onStageResize(event:DataEvent) : void
        {
            this.stageResize();
            return;
        }// end function

        override public function stageResize() : void
        {
            super.stageResize();
            x = Global.stage.stageWidth - 216 - 150;
            y = 170 - 44 + 23 + 20 * (Global.stage.stageHeight / 1000);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.stageResize();
            return;
        }// end function

        public function set isTakeAvailable(param1:Boolean) : void
        {
            if (param1)
            {
                this.addAroundEffect();
            }
            else
            {
                this.removeAroundEffect();
            }
            return;
        }// end function

        private function addAroundEffect() : void
        {
            this._swfPlayer.timeRate = 3;
            this._swfPlayer.move(28 + 42, 27 + 27);
            this._swfPlayer.load("ActionIconEffect.swf", ModelType.NormalSwf, null);
            this.contentTopOf3DSprite.addChild(this._swfPlayer);
            return;
        }// end function

        private function removeAroundEffect() : void
        {
            if (this._swfPlayer.parent)
            {
                this.contentTopOf3DSprite.removeChild(this._swfPlayer);
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._ball.dispose();
            this._ball = null;
            this._blessNum.dispose();
            this._blessNum = null;
            this._clickSpr.dispose();
            this._clickSpr = null;
            this._bmp.dispose();
            this._bmp = null;
            this.removeEffect();
            Rect3DManager.instance.disposeRect3d(this._rect3d);
            this._rect3d = null;
            this._swfPlayer.dispose();
            this._swfPlayer = null;
            this.removeListeners();
            return;
        }// end function

        public static function get instance() : FriendBlessBall
        {
            if (_instance == null)
            {
                _instance = new FriendBlessBall;
            }
            return _instance;
        }// end function

        public static function hasInstanceShowing() : Boolean
        {
            if (_instance == null)
            {
                return false;
            }
            return !_instance.isHide;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
